<template>
  <div class="app-container">
    <el-card shadow="always">
      <el-tabs v-model="activeName">
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" name="first">
          <el-button type="primary" @click="handlerAdd">新增角色</el-button>
          <el-table v-loading="loading" style="width: 100%" :data="list">
            <el-table-column
              type="index"
              label="序号"
              width="100"
            />
            <el-table-column
              prop="name"
              label="角色名称"
              width="240"
            />
            <el-table-column
              prop="description"
              label="描述"
            />
            <el-table-column
              label="操作"
              width="320"
            >
              <template slot-scope="{ row }">
                <el-button size="small" type="success" @click="handleAssign(row)">分配权限</el-button>
                <el-button size="small" type="primary" @click="handlerEdit(row)">编辑</el-button>
                <el-button size="small" type="danger" @click="handlerDel(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="end" align="middle" style="height: 60px">
            <el-pagination
              background
              :current-page.sync="params.page"
              :page-size.sync="params.pagesize"
              :page-sizes="[1,3,5,7,10]"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="getRole"
              @current-change="getRole"
            />
          </el-row>
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <role-dialog ref="roleDialog" :dialog-visible.sync="dialogVisible" />
    <manager-permission :dialog-visible.sync="dialogVisible1" :row-id="rowId" />
  </div>
</template>

<script>
import { getRoleApi, deleteRoleApi, getCompanyInfoApi } from '@/api/setting'
import RoleDialog from './components/RoleDialog.vue'
import { mapGetters } from 'vuex'
import ManagerPermission from './components/manager-permission.vue'
export default {
  name: 'SettingIndex',
  components: { RoleDialog, ManagerPermission },

  data() {
    return {
      activeName: 'first',
      params: {
        page: 1,
        pagesize: 10
      },
      list: [],
      total: 0,
      loading: false, // 加载中控制器
      dialogVisible: false, // 控制弹窗显隐
      formData: {}, // 公司管理数据
      dialogVisible1: false,
      rowId: '' // 当前点击的角色ID
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  watch: {
    // 检测点击的是哪个tab栏，当点击第二个时才让他请求
    activeName(val) {
      if (val === 'second' && !this.formData.id) {
        this.getCompanyInfo()
      }
    }
  },
  created() {
    this.getRole()
    // this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRole() {
      try {
        this.loading = true
        const { rows, total } = await getRoleApi(this.params)
        this.list = rows
        this.total = total
        // 如果total不为零但当前页数据为0的情况，就往前跳一页
        if (total > 0 && rows.length === 0) {
          // 往前推一页
          this.params.page--
          this.getRole()
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },
    // 添加逻辑
    handlerAdd() {
      this.dialogVisible = true
    },
    // 处理编辑逻辑
    handlerEdit(row) {
      this.handlerAdd()
      this.$refs.roleDialog.roleForm = { ...row }
      // $children不推荐
    },
    // 处理删除逻辑
    handlerDel(id) {
      // try {
      //   await this.$confirm('张雨豪来一条毛线红裤衩吗？', '提示', {
      //     type: 'warning',
      //     confirmButtonText: '来一条',
      //     cancelButtonText: '来两条'
      //   })
      //   // 确定的逻辑
      //   console.log('确定', id)
      // } catch (error) {
      //   console.log('取消的逻辑')
      // }
      this.$confirm('您确定要删除吗？', '提示', {
        type: 'warning'
      }).then(() => {
        return deleteRoleApi(id)
      }).then(() => {
        console.log('删除成功')
        this.$message.success('删除成功√')
        this.getRole()
      }).catch(err => {
        console.log(err)
      })
    },
    // 获取公司信息
    async getCompanyInfo() {
      const companyInfo = await getCompanyInfoApi(this.companyId)
      this.formData = companyInfo
    },
    // 打开权限分配窗口
    handleAssign(row) {
      this.dialogVisible1 = true
      this.rowId = row.id
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

